
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dto.CarDetailDTO;
import east2west.dto.CarsDTO;
import east2west.service.CarService;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author anhtuan
 */
public class uploadAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        boolean flag = true;
        javax.servlet.ServletConfig config = servlet.getServletConfig();
        CarService serCar = new CarService();
        CarsDTO carDTO = new CarsDTO();
        ArrayList<CarDetailDTO> listDetail = new ArrayList<CarDetailDTO>();
        //upload anh
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
        } else {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            Iterator itr = items.iterator();
            while (itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (name.equals("carName")) {
                        carDTO.setCarName(item.getString());
                    }
                    if (name.equals("detail")) {
                        carDTO.setDetail(item.getString());
                    }
                    if (name.equals("model")) {
                        int modeId = Integer.parseInt(item.getString());
                        carDTO.setModelId(modeId);
                    }
                    if (name.equals("type")) {
                        int typeId = Integer.parseInt(item.getString());
                        carDTO.setTypeId(typeId);
                    }
                    if (name.equals("air")) {
                        byte air = Byte.parseByte(item.getString());
                        carDTO.setAirConditional(air);
                    }
                    if (name.equals("quantity")) {
                        int quantity = Integer.parseInt(item.getString());
                        carDTO.setQuantity(quantity);
                    }
                    if (name.equals("seatQty")) {
                        int seatQty = Integer.parseInt(item.getString());
                        carDTO.setSeatQty(seatQty);
                    }
                    if (name.equals("price")) {
                        float price = Float.parseFloat(item.getString());
                        carDTO.setPrice(price);
                    }

                } else {
                    try {
                        String itemName = item.getName();
                        CarDetailDTO detailDTO = new CarDetailDTO();
                        detailDTO.setImageUrl("images/car/" + itemName);
                        listDetail.add(detailDTO);
                        String url = config.getServletContext().getRealPath("/");
                        String url1 = url.substring(0, (url.length() - 10));
                        File savedFile = new File(url1 + "/web/images/car/" + itemName);
                        item.write(savedFile);
                        request.setAttribute("url", url1);

                    } catch (Exception e) {
                        flag = false;
                    }
                }
            }
        }
        //end
        boolean flagInsert = true;
        if (flag) {
            int carId = serCar.createCar(carDTO);
            if (carId > 0) {
                for (Iterator<CarDetailDTO> it = listDetail.iterator(); it.hasNext();) {
                    CarDetailDTO carDetailDTO = it.next();
                    carDetailDTO.setCarId(carId);
                    if (!serCar.createCarDetail(carDetailDTO)) {
                        flagInsert = false;
                    }
                }

            }
        }
        request.setAttribute("page", "carAdd.page");
        request.setAttribute("carDTO", carDTO);
        request.setAttribute("detail", listDetail);
        return mapping.findForward(SUCCESS);
    }
}
